
//
// 120326 - AH - (Bergen Off. Bibliotek *) Created.
// 120517 - AH - (Hurray) (Huset) Made the created engine an attribute of the 
//               orb.
// 120521 - AH - (Huset) Renamed from SoekratEngineFactory since this do make
//               a Soekrat2 engine.
// 120613 - AH - Created as OrbInitializer from old deprecated 'suite 
//               initializer framework'.
// 120618 - AH - (Bergen Off. Bibliotek) Removed the creation of the database
//               from here to its own supplier.
//

package org.soekrat.server.orbsuppliers.s2;

import java.io.IOException;
import java.util.List;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.SoekratEngine;
import org.soekrat.environment.Orb;
import org.soekrat.environment.OrbSupplier;
import org.soekrat.environment.conversationbase.SoekratDatabase;

/**
 * <p>
 *   Object that creates a
 *   {@link Soekrat2Engine} as the
 *   {@link SoekratEngine} to put on the
 *   {@link Orb} as an attribute named {@code soekrat-engine}.
 * </p>
 * <p>
 *   Requires that the
 *   {@link SoekratDatabase} has been created and can be found on the 
 *   {@link Orb} as an attribute named {@code database}. 
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class Soekrat2EngineOrbSupplier extends S2OrbSupplier 
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public Soekrat2EngineOrbSupplier(){}
  
  // Specified in S2OrbInitializer.
  public String supply(Orb orb, List<OrbSupplier> secondPass)
  {
    if (orb==null)
    {
      throw new NullPointerException("orb");
    }
    
    try
    {
      SoekratDatabase sd=(SoekratDatabase)orb.getAttribute("database");
      if (sd==null)
      {
        return "Soekrat2EngineOrbSupplier: "+" Missing database";
      }
      
      SoekratEngine engine=new Soekrat2Engine(sd);
      orb.setAttribute("soekrat-engine", engine);
    
      return null;
    }
    catch (IOException iox)
    {
      String msg=iox.getLocalizedMessage();
      return msg==null ? "Soekrat2EngineOrbSupplier: failed to create engine" : 
                         "Soekrat2EngineOrbSupplier: "+msg;
    }
  }
  
}
